package com.sina.shop.controller.address;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.address.Address;
import com.sina.shop.service.address.AddressService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:  处理外部请求controller
 * 段浩杰   2018-01-31-17:51
 */
@Controller
@RequestMapping("/shop-web/address")//shop-web此映射路径为了匹配路由
public class AddressController extends BaseController {

    @Autowired
    private AddressService addressService;

    @RequestMapping(value = "/getAddressList", method = RequestMethod.GET)
    public ModelAndView getAddressList(Long adminId) {

        LOG.info("adminId:{}", adminId);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        List<Address> addressList = addressService.getAddressList(adminId);

        data.put("addressList", addressList);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
    public ModelAndView deleteAddress(Long id) {
        LOG.info("id:{}", id);
        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("commonView");

        try {
            addressService.deleteAddress(id);
            result.put(CODE, OK);
            result.put(MSG, "删除地址成功");
        } catch (UpdateException e) {
            LOG.info("删除地址出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    public ModelAndView updateAddress(Address address) {
        LOG.info("address:{}", address);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            addressService.updateAddress(address);
            result.put(CODE, OK);
            result.put(MSG, "更新地址成功");
        } catch (UpdateException e) {
            LOG.info("更新地址出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/updateAddressNot", method = RequestMethod.POST)
    public ModelAndView updateAddressNot(@RequestParam("aid") Long aid) {
        LOG.info("aid:{}", aid);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            addressService.updateAddressNot(aid);
            result.put(CODE, OK);
            result.put(MSG, "更新地址成功");
        } catch (UpdateException e) {
            LOG.info("更新地址不可用出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/updateAddressUse", method = RequestMethod.POST)
    public ModelAndView updateAddressUse(@RequestParam("id") Long id) {
        LOG.info("ad:{}", id);

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            addressService.updateAddressUse(id);
            result.put(CODE, OK);
            result.put(MSG, "更新地址成功");
        } catch (UpdateException e) {
            LOG.info("更新地址可用出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/saveAddress", method = RequestMethod.POST)
    public ModelAndView saveAddress(Address address) {
        LOG.info("address:{}", address);
        address.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            addressService.saveAddress(address);
            result.put(CODE, OK);
            result.put(MSG, "新增地址成功");
        } catch (UpdateException e) {
            LOG.info("新增地址出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }
}
